home *** CD-ROM | disk | FTP | other *** search
- /*
- * SFskyedit - Star Fighter 3000 sky colours editor
- * SkyCols savebox
- * Copyright (C) 2001 Chris Bazley
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public Licence as published by
- * the Free Software Foundation; either version 2 of the Licence, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public Licence for more details.
- *
- * You should have received a copy of the GNU General Public Licence
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- */
-
- /* ANSI library files */
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <stdbool.h>
-
- /* RISC OS library files */
- #include "wimp.h"
- #include "toolbox.h"
- #include "event.h"
- #include "wimplib.h"
- #include "saveas.h"
- #include "dcs.h"
-
- /* My library files */
- #include "err.h"
- #include "msgtrans.h"
- #include "Macros.h"
- #include "SFformats.h"
- #include "FilePerc.h"
- #include "Loader.h"
-
- /* Local headers */
- #include "EditSky.h"
- #include "SFSSaveBox.h"
- #include "utils.h"
- #include "DCS_dialogue.h"
-
- ObjectId savebox_sharedid = NULL_ObjectId;
-
- /* ----------------------------------------------------------------------- */
- /* Function prototypes */
-
- static ToolboxEventHandler _SaveFile_savecompleted, _SaveFile_savehandler, _SaveFile_abouttoopen, _SaveFile_buttonshandler;
- static void set_filename(ViewData *view_data);
-
- /* ----------------------------------------------------------------------- */
- /* Public functions */
-
- void SaveFile_initialise(IdBlock *id_block)
- {
- savebox_sharedid = id_block->self_id;
-
- ObjectId savebox_windowid;
- EF(saveas_get_window_id(0, savebox_sharedid, &savebox_windowid));
- EF(event_register_toolbox_handler(savebox_windowid, ActionButton_Selected, _SaveFile_buttonshandler, NULL));
-
- EF(event_register_toolbox_handler(savebox_sharedid, SaveAs_AboutToBeShown, _SaveFile_abouttoopen, NULL));
- EF(event_register_toolbox_handler(savebox_sharedid, SaveAs_SaveCompleted, _SaveFile_savecompleted, NULL));
- EF(event_register_toolbox_handler(savebox_sharedid, SaveAs_SaveToFile, _SaveFile_savehandler, NULL));
- }
-
- /* ----------------------------------------------------------------------- */
- /* Private functions */
-
- static int _SaveFile_buttonshandler(int event_code, ToolboxEvent *event, IdBlock *id_block, void *handle)
- {
- ActionButtonSelectedEvent *abse = (ActionButtonSelectedEvent *)event;
- if(id_block->self_component != 0x82bc02 || !FLAG_SET(abse->hdr.flags, ActionButton_Selected_Adjust))
- return 0; /* not interested */
-
- /* Cancel button clicked on underlying window - reset dialogue box */
- ViewData *view_data;
- ObjectId sb_ancestor;
- E_RETV(toolbox_get_ancestor(0, savebox_sharedid, &sb_ancestor, NULL), 1)
- if(!E(toolbox_get_client_handle(0, sb_ancestor, (void **)&view_data)))
- set_filename(view_data);
-
- return 1; /* claim event */
- }
-
- /* ----------------------------------------------------------------------- */
-
- static int _SaveFile_abouttoopen(int event_code, ToolboxEvent *event, IdBlock *id_block, void *handle)
- {
- /* Set up dialogue box for ancestor document */
- ViewData *view_data;
- if(!E(toolbox_get_client_handle(0, id_block->ancestor_id, (void **)&view_data)))
- set_filename(view_data);
-
- return 0; /* pass this event on */
- }
-
- /* ----------------------------------------------------------------------- */
-
- static int _SaveFile_savehandler(int event_code, ToolboxEvent *event, IdBlock *id_block, void *handle)
- {
- SaveAsSaveToFileEvent *sastf = (SaveAsSaveToFileEvent *)event;
- ViewData *view_data;
- _kernel_oserror *err;
-
- err = toolbox_get_client_handle(0, id_block->ancestor_id, (void **)&view_data);
- if(err != NULL)
- goto SaveFail;
-
- /* Save Fednet file */
- err = perc_operation(FILEPERC_OP_COMP, sastf->filename, FILETYPE_SKYCOLS, (flex_ptr)&view_data->sky);
- if(err != NULL) /* Saving error */
- goto SaveFail;
-
- RE(saveas_file_save_completed(1, id_block->self_id, sastf->filename));
- return 1; /* claim event */
-
- SaveFail:
- err_report(err->errnum, msgs_lookup_sub1("SaveFail", err->errmess));
- RE(saveas_file_save_completed(0, id_block->self_id, sastf->filename));
- return 1; /* claim event */
- }
-
- /* ----------------------------------------------------------------------- */
-
- static int _SaveFile_savecompleted(int event_code, ToolboxEvent *event, IdBlock *id_block, void *handle)
- {
- SaveAsSaveCompletedEvent *sasc = (SaveAsSaveCompletedEvent *)event;
- ViewData *view_data;
-
- E_RETV(toolbox_get_client_handle(0, id_block->ancestor_id, (void **)&view_data), 1)
-
- if(FLAG_SET(sasc->hdr.flags, SaveAs_DestinationSafe)) {
- /* Mark data as saved */
- char *buf;
- if(!E(loader_canonicalise(&buf, NULL, NULL, sasc->filename))) {
- EditSky_newfile(view_data, buf, true);
- free(buf);
- } else
- EditSky_newfile(view_data, sasc->filename, true);
-
- /* If we were opened from the DCS dbox, close the document window */
- if(id_block->parent_id == dcs_sharedid) {
-
- if(dcs_openparent) /* (set if ADJUST-click on document's close icon) */
- EditSky_openparentdir(view_data); /* open parent directory of file */
-
- RE(toolbox_delete_object(0, id_block->ancestor_id))
- }
- }
- return 1; /* claim event */
- }
-
- /* ----------------------------------------------------------------------- */
-
- static void set_filename(ViewData *view_data)
- {
- char *filename;
- if(strcmp(view_data->last_savepath, "<untitled>") == 0)
- filename = "SkyCols"; /* invent sensible leafname */
- else
- /* use existing title (whether leafname or full path) */
- filename = view_data->last_savepath;
- RE(saveas_set_file_name(0, savebox_sharedid, filename))
- }
-